<?php

/*
 * Action - 5$
 *
 * Take 2 coin tokens.
 * You may trash a card from your hand and then pay any number of coin tokens.
 * If you did trash a card ,gain a card with a cost uf up to the cost of the trashed card plus 
 * the number of Coin tokens you paid.
 *
 */

class Butcher extends Card implements iAction, iKingdom, iGuilds, iCoinTokensRequired {

    public function __construct() {
        parent::__construct("Butcher", 5);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "Take 2 coin tokens.",
            "You may trash a card from your hand and then pay any number of coin tokens",
            "If you did trash a card, gain a card with a cost of up to the cost of the trashed card plus the number of coin tokens you paid.",
        );
    }

    public function play(Game $game, $isRealCard) {

        $game->addCoinTokens(2);
        ScreenDisplay::addScreenMessage($game, true, "take 2 coin tokens ", "takes 2 coin tokens");
        $playhand = $game->getPlayHand();
        if (sizeof($playhand) > 0) {

            $options = getUniqueCards($playhand);
            $options[] = new Option(1, "None");
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Choose a card to trash, or none");
            $old_card_name = $output[0];

            if ($old_card_name != "None") {

                $old_card = $game->moveCardFromPileToPile($old_card_name, PlayHand, Trash);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($old_card), "trashes " . ScreenDisplay::$LMSG);

                
                $coinTokens = $game->getCoinTokens();
                $options = array();
                for ($i = 0; $i <= $coinTokens; $i++) {
                    $options[] = new Option($i, "$i");
                }
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "How many coin tokens to play");
                $coinTokensChosen = intval($output[0]);

                $game->addCoinTokens(-1 * $coinTokensChosen);
                ScreenDisplay::addScreenMessage($game, true, "pay $coinTokensChosen coin tokens ", "pays $coinTokensChosen coin tokens");

                $price = $old_card->getPrice($game);
                $new_price = $price->add($coinTokensChosen);
                
                $optional_cards = $game->getBoardCardsByPrice(new Price(), $new_price);
                if (sizeof($optional_cards) > 0) {
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain");

                    $new_card_name = $output[0];
                    $new_card = $game->moveCardFromPileToPile($new_card_name, BoardCards, DiscardPile);
                    ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
                }
            } else {
                ScreenDisplay::addScreenMessage($game, false, " and trash nothing", " and trashes nothing");
            }
        } else {
            ScreenDisplay::addScreenMessage($game, false, " and trash nothing", " and trashes nothing");
        }

        $game->cardActionFinished();
        return true;
    }

}

?>